package com.example.web.service.cabinetstatus.impl;

import com.example.web.service.TimeIteratorNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Pattern;

@Slf4j
@Order(1)
@Component
public class UTCIteratorNodeImpl extends TimeIteratorNode {
    @Override
    public boolean condition(String timeStr) {
        log.info("check utc");
        String pattern = "^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\\.[0-9]+)?(Z)?$";
        return Pattern.matches(pattern, timeStr);
    }

    @Override
    public LocalDateTime parse(String timeStr) {
        log.info("use utc");
        DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_DATE_TIME;
        return LocalDateTime.parse(timeStr, isoDateTime);
    }
}
